package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.NewsDao;
import com.khyr.entity.News;
import com.khyr.service.NewsService;
import com.khyr.vo.NewsVo;
import com.khyr.vo.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsDao newsDao;
    @Override
    public R<Page<News>> getNewsByParams(NewsVo newsVo) {
        //分页
        Page<News> page = new Page<>(newsVo.getPage(), newsVo.getPageSize());
        //模糊
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(newsVo.getNewsSource())){
            queryWrapper.like("news_source", newsVo.getNewsSource());
        }
        if (StringUtils.hasText(newsVo.getStartTime())&& StringUtils.hasText(newsVo.getEndTime())){
            queryWrapper.between("create_time", newsVo.getStartTime(), newsVo.getEndTime());
        }
        Page<News> PageInfo = newsDao.selectPage(page, queryWrapper);
        if (PageInfo != null){
            return new R<>(200,"查询成功", PageInfo);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<String> addNews(News news) {
        int i = newsDao.insert(news);
        if (i > 0){
            return new R<>(200,"添加成功", null);
        }
        return new R<>(500,"添加失败", null);
    }

    @Override
    public R<String> updateNews(News news) {
        int i = newsDao.updateById(news);
        if (i > 0){
            return new R<>(200,"修改成功", null);
        }
        return new R<>(500,"修改失败", null);
    }

    @Override
    public R<String> deleteNewsByIds(Long newsId) {
        if (newsId != null){
            int i = newsDao.deleteById(newsId);
            if (i > 0){
                return new R<>(200,"删除成功", null);
            }
        }
        return new R<>(500,"删除失败", null);
    }
}
